package com.ym.controller.management;

import com.github.pagehelper.PageInfo;

import com.ym.common.utils.page.PageDomain;
import com.ym.core.annotation.Log;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.core.exception.BusinessException;
import com.ym.core.utils.tkmybatis.TK;

import com.ym.medical.domain.entity.EdBodyGuidEntity;
import com.ym.medical.domain.entity.StandardDeptEntity;
import com.ym.medical.service.IEdBodyGuidSV;
import com.ym.medical.service.IStandardDeptSV;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.List;

@Api(tags = "身体部位数据管理")
@RequestMapping("management/body/position")
@RestController
@Validated
public class PositionFindController extends BaseController {

	@Autowired
	private IEdBodyGuidSV edBodyGuidSV;

	@Autowired
    private IStandardDeptSV iStandardDeptSV;

	@Log(dataBase = false, printBody = false)
	@ApiOperation("部位下拉框")
	@GetMapping(value = "/dropDown")
	public APIResponse<List<EdBodyGuidEntity>> dropDown(
	) {
		List<EdBodyGuidEntity> data = edBodyGuidSV
				.selectList(TK.select(EdBodyGuidEntity.class)
						.distinct()
						.column("position").where()
						.andIsNotNull(EdBodyGuidEntity::getIcon)
						.end().orderBy(EdBodyGuidEntity::getSortId)
				);
		return successData(data);
	}

	@ApiOperation("列表")
	@GetMapping(value = "/list")
	public APIResponse<PageInfo<EdBodyGuidEntity>> list(
			EdBodyGuidEntity query) {
		PageDomain.startPage();
		List<EdBodyGuidEntity> data = edBodyGuidSV
				.selectList(query);
		return successPage(data);
	}

	@ApiOperation("新增修改")
	@PostMapping(value = "/update", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public APIResponse operationManualUpdate(
			@RequestBody EdBodyGuidEntity edBodyGuid
	) {
		if (edBodyGuid.getId() != null && edBodyGuid.getId() >-1) {
			edBodyGuidSV.updateById(edBodyGuid);
		} else {
			edBodyGuidSV.insert(edBodyGuid);
			//序号设置为ID
			EdBodyGuidEntity updateSort = new EdBodyGuidEntity();
			updateSort.setId(edBodyGuid.getId());
			updateSort.setSortId(edBodyGuid.getId().intValue());
			edBodyGuidSV.updateById(updateSort);
		}
		return success();
	}

	@ApiOperation("部位图标管理列表")
	@GetMapping(value = "/icon/list")
	public APIResponse iconList(
	) {

		List<EdBodyGuidEntity> data = edBodyGuidSV
				.selectList(TK.select(EdBodyGuidEntity.class).distinct()
				.column("position", "icon").where().end().orderBy(EdBodyGuidEntity::getSortId));
		return successData(data);
	}

	@ApiOperation("部位图标修改")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "部位", name = "position", paramType = "form"),
			@ApiImplicitParam(value = "图标", name = "icon", paramType = "form")
	})
	@PostMapping(value = "/icon/update")
	public APIResponse iconUpdate(
			@NotEmpty String position,
			@NotEmpty String icon
	) {
		EdBodyGuidEntity update = new EdBodyGuidEntity();
		update.setIcon(icon);
		edBodyGuidSV.updateByParams(update,TK.selectWhere(EdBodyGuidEntity.class)
				.andEqualTo(EdBodyGuidEntity::getPosition,position).end());
		return success();
	}

	@ApiOperation("删除")
	@PostMapping(value = "/delete", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public APIResponse operationManualDelete(
			@RequestBody Long[] ids
	) {
		if(ids == null || ids.length <= 0){
			throw new BusinessException("请至少选择一个");
		}

		int flag = edBodyGuidSV.deleteByIds(ids);
		if(flag >0 ){
			return successMsg("删除成功");
		}
		return successMsg("删除失败");
	}


	@ApiOperation("部位科室列表")
	@GetMapping(value = "/dept/list")
	@ResponseBody
	public APIResponse deptList(){
		List<StandardDeptEntity> depts =  iStandardDeptSV
				.selectList(
						TK.select(StandardDeptEntity.class)
								.column("id","dept_code","dept_name","dept_img_url","partent_dept_code"
								,"dept_level","dept_sort")
						.where().andNotEqualTo(StandardDeptEntity::getDeptCode,"-1").end()
				);

		return APIResponse.success(depts);
	}
}
